package cn.study.controller;


import cn.study.entity.*;
import cn.study.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 单选试题表 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Controller
@Slf4j
@RequestMapping("/study-test-questions-one")
public class StudyTestQuestionsOneController {

    @Autowired
    StudyTestPaperParticularsService studyTestPaperParticularsService;
    @Autowired
    StudyTestPaperService studyTestPaperService;
    @Autowired
    StudyDepartmentService studyDepartmentService;
    @Autowired
    StudyTestQuestionsOneService studyTestQuestionsOneService;
    @Autowired
    StudyTestQuestionsMoreService studyTestQuestionsMoreService;
    @Autowired
    StudyTestQuestionsShortanswerService studyTestQuestionsShortanswerService;
    @RequestMapping("/studyQuestionsList")
    public String studyQuestionsList(
                                     Model model,@RequestParam(value="pnone",defaultValue="1") Integer pnone,
                                     @RequestParam(value="paginalsizeone",defaultValue = "5") Integer paginalsizeone,
                                     @RequestParam(value="pnmore",defaultValue = "1") Integer pnmore,
                                     @RequestParam(value="paginalsizemore",defaultValue = "5") Integer paginalsizemore,
                                     @RequestParam(value="pnshort",defaultValue = "1") Integer pnshort,
                                     @RequestParam(value="paginalsizeshort",defaultValue = "5") Integer paginalsizeshort,
                                     @RequestParam(value="oneval",defaultValue = "") String oneval,
                                     @RequestParam(value="moreval",defaultValue = "") String moreval,
                                     @RequestParam(value="shoreval",defaultValue = "") String shoreval,
                                     @RequestParam(value="oms",defaultValue = "one") String oms
                                     ){
        /*单选查询=============*/
        PageHelper.startPage(pnone, paginalsizeone);
        List<StudyTestQuestionsOne> onelist=studyTestQuestionsOneService.oneLikeList(oneval);
        PageInfo pInfoOne=new PageInfo(onelist,10);
        /*多选查询===========*/
        PageHelper.startPage(pnmore, paginalsizemore);
        List<StudyTestQuestionsMore> morelist=studyTestQuestionsMoreService.moreLikeList(moreval);
        PageInfo pInfoMore=new PageInfo(morelist,10);
        /*==================*/
        /*简答查询===========*/
        PageHelper.startPage(pnshort, paginalsizeshort);
        List<StudyTestQuestionsShortanswer> shorlist=studyTestQuestionsShortanswerService.shortLikeList(shoreval);
        PageInfo pInfoShor=new PageInfo(shorlist,10);
        /*==================*/
        model.addAttribute("pInfoOne",pInfoOne);
        model.addAttribute("pInfoMore",pInfoMore);
        model.addAttribute("pInfoShor",pInfoShor);
        model.addAttribute("paginalsizeone",paginalsizeone);
        model.addAttribute("paginalsizemore",paginalsizemore);
        model.addAttribute("paginalsizeshort",paginalsizeshort);
        model.addAttribute("oneval",oneval);
        model.addAttribute("moreval",moreval);
        model.addAttribute("shoreval",shoreval);
        model.addAttribute("oms",oms);
        return "study_questions";
    }
    @RequestMapping("/study_shijuan_add")
    public String study(
            Model model,@RequestParam(value="pnone",defaultValue="1") Integer pnone,
            @RequestParam(value="paginalsizeone",defaultValue = "5") Integer paginalsizeone,
            @RequestParam(value="pnmore",defaultValue = "1") Integer pnmore,
            @RequestParam(value="paginalsizemore",defaultValue = "5") Integer paginalsizemore,
            @RequestParam(value="pnshort",defaultValue = "1") Integer pnshort,
            @RequestParam(value="paginalsizeshort",defaultValue = "5") Integer paginalsizeshort,
            @RequestParam(value="oneval",defaultValue = "") String oneval,
            @RequestParam(value="moreval",defaultValue = "") String moreval,
            @RequestParam(value="shoreval",defaultValue = "") String shoreval,
            @RequestParam(value="oms",defaultValue = "one") String oms
    ){
        /*单选查询=============*/
        PageHelper.startPage(pnone, paginalsizeone);
        List<StudyTestQuestionsOne> onelist=studyTestQuestionsOneService.oneLikeList(oneval);
        PageInfo pInfoOne=new PageInfo(onelist,10);
        /*=================*/
        /*试卷id=================*/
        List<StudyTestPaper> pagelist=studyTestPaperService.getlistid();
        model.addAttribute("pagelist", pagelist);
        /*多选查询===========*/
        PageHelper.startPage(pnmore, paginalsizemore);
        List<StudyTestQuestionsMore> morelist=studyTestQuestionsMoreService.moreLikeList(moreval);
        PageInfo pInfoMore=new PageInfo(morelist,10);
        /*==================*/
        /*简答查询===========*/
        PageHelper.startPage(pnshort, paginalsizeshort);
        List<StudyTestQuestionsShortanswer> shorlist=studyTestQuestionsShortanswerService.shortLikeList(shoreval);
        PageInfo pInfoShor=new PageInfo(shorlist,10);
        /*==================*/
        model.addAttribute("pInfoOne",pInfoOne);
        model.addAttribute("pInfoMore",pInfoMore);
        model.addAttribute("pInfoShor",pInfoShor);
        model.addAttribute("paginalsizeone",paginalsizeone);
        model.addAttribute("paginalsizemore",paginalsizemore);
        model.addAttribute("paginalsizeshort",paginalsizeshort);
        model.addAttribute("oneval",oneval);
        model.addAttribute("moreval",moreval);
        model.addAttribute("shoreval",shoreval);
        model.addAttribute("oms",oms);
        return "study_shijuan_add";
    }
    @GetMapping("/getsave")
    public String getadd(Model model){
        model.addAttribute("deplist",studyDepartmentService.list());
        return "study_questions_add";
    }
    @PostMapping("/save")
    public String save(StudyTestQuestionsOne studyTestQuestionsOne){
        studyTestQuestionsOneService.save(studyTestQuestionsOne);
        return "redirect:/study-test-questions-one/studyQuestionsList";
    }
 /*   @GetMapping("/study_shijuan")
    public String getaddd(ModelMap modelmap){
        modelmap.put("pInfoOne",studyTestPaperService.getlist());
        return "study_shijuan_add";
    }*/
    @PostMapping("/savee")
    public String savee(String checkedid,Integer pageid ){
        String[] oneIds=checkedid.split(",");
        List<Integer> list=new ArrayList<>();
     for(String s:oneIds){
         list.add(Integer.parseInt(s));
     }
     List<StudyTestQuestionsOne> list1=studyTestQuestionsOneService.listByIds(list);
        List<StudyTestPaperParticulars> list2=new ArrayList<>();
        for (StudyTestQuestionsOne s:list1){
            StudyTestPaperParticulars aaa=new StudyTestPaperParticulars();
            aaa.setTestPaperNum(s.getId());
            aaa.setTestQuestionsOptiona(s.getTestQuestionsOneOptiona());
            aaa.setTestQuestionsOptionb(s.getTestQuestionsOneOptionb());;
            aaa.setTestQuestionsOptionc(s.getTestQuestionsOneOptionc());;
            aaa.setTestQuestionsOptiond(s.getTestQuestionsOneOptiond());;
            aaa.setTestPaperOpic(s.getTestQuestionsOneOpic());
            aaa.setOptionOrAnswer(s.getTestQuestionsOneAnswer());
            aaa.setTestPaperScore(10);
            aaa.setTestType(1);
            aaa.setTestPaperId(pageid);
            list2.add(aaa);
        }
        studyTestPaperParticularsService.saveBatch(list2);
        return "redirect:/study-test-paper/shijuanlist";
}
    @GetMapping("del")
    public String del(Integer id)
    {
        boolean bl=studyTestQuestionsOneService.removeById(id);

        if(bl){
            log.info("删除成功");
        }else {
            log.info("删除失败");
        }
        return "redirect:/study-test-questions-one/studyQuestionsList";
    }
    @GetMapping("/upd")
    public String upd(Integer id,Model model)
    {
        StudyTestQuestionsOne list=studyTestQuestionsOneService.getById(id);
        model.addAttribute("list",list);


        return "study_TestQuestionOne_upd";
    }

    @PostMapping("/oneupd")
    public String roleupd(Model model, StudyTestQuestionsOne one)
    {
        studyTestQuestionsOneService.updateById(one);

        return "redirect:/study-test-questions-one/studyQuestionsList";
    }
}

